﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Assignment3
{
    public enum WallDirection
    {
        North,
        South,
        East,
        West
    }

    public class Wall : BasicShape
    {
        public WallDirection direction;

        public BoundingBox bounds;

        public Wall() : base() { }

        public Wall(Vector3 size, Vector3 position)
            : base(size, position) { }

        public Wall(Vector3 size, Vector3 position, WallDirection direction)
            : base(size, position) 
        {
            this.direction = direction;

            if(direction == WallDirection.East || direction == WallDirection.West)
                this.world = Matrix.CreateRotationY(MathHelper.ToRadians(90.0f));
            
        }

        public void HandleCollision(ref Vector3 position, Vector3 lastPosition)
        {
            switch (this.direction)
            {
                case WallDirection.North:
                    if(this.bounds.Contains(position) == ContainmentType.Contains)
                        position.Z = lastPosition.Z;
                    break;

                case WallDirection.East:
                    if (this.bounds.Contains(position) == ContainmentType.Contains)
                        position.X = lastPosition.X;
                    break;
                case WallDirection.South:
                    if (this.bounds.Contains(position) == ContainmentType.Contains)
                        position.Z = lastPosition.Z;
                    break;
                case WallDirection.West:
                    if (this.bounds.Contains(position) == ContainmentType.Contains)
                        position.X = lastPosition.X;
                    break;
            }
        }

        public override void BuildShape()
        {
            primitiveCount = 2;
            vertices = new VertexPositionNormalTexture[6];

            Vector2[] texCoords = new Vector2[6];

            texCoords[0] = new Vector2(0, 0);
            texCoords[1] = new Vector2(0, 1);
            texCoords[2] = new Vector2(1, 0);

            texCoords[3] = new Vector2(0, 1);
            texCoords[4] = new Vector2(1, 1);
            texCoords[5] = new Vector2(1, 0);

            Vector3[] face = new Vector3[6];
            //TopLeft
            face[0] = new Vector3(-size.X, size.Y, 0.0f);
            //BottomLeft
            face[1] = new Vector3(-size.X, -size.Y, 0.0f);
            //TopRight
            face[2] = new Vector3(size.X, size.Y, 0.0f);
            //BottomLeft
            face[3] = new Vector3(-size.X, -size.Y, 0.0f);
            //BottomRight
            face[4] = new Vector3(size.X, -size.Y, 0.0f);
            //TopRight
            face[5] = new Vector3(size.X, size.Y, 0.0f);

            //face
            for (int i = 0; i <= 2; i++)
            {
                vertices[i] = new VertexPositionNormalTexture(face[i] + /*Vector3.UnitZ * size.Z +*/ position, Vector3.UnitZ, texCoords[i]);
                vertices[i + 3] = new VertexPositionNormalTexture(face[i + 3] + /*Vector3.UnitZ * size.Z +*/ position, Vector3.UnitZ, texCoords[i + 3]);
            }


        }

        public void CreateBoundingBox()
        {
            Vector3[] vertices = new Vector3[4];
            if(direction == WallDirection.North || direction == WallDirection.South)
            {
                vertices[0] = this.vertices[1].Position;
                vertices[1] = this.vertices[2].Position;

                vertices[2] = vertices[0] + new Vector3(0.0f, 0.0f, 1f);
                vertices[3] = vertices[1] + new Vector3(0.0f, 0.0f, -1f);
            }
            if (direction == WallDirection.East || direction == WallDirection.West)
            {
                vertices[0] = this.vertices[1].Position;
                vertices[1] = this.vertices[2].Position;

                vertices[2] = vertices[0] + new Vector3(0.0f, 0.0f, 1f);
                vertices[3] = vertices[1] + new Vector3(0.0f, 0.0f, -1f);


                for (int i = 0; i < 4; i++)
                {
                    vertices[i] -= position;

                    vertices[i] = Vector3.Transform(vertices[i], Matrix.CreateRotationY(MathHelper.ToRadians(90.0f)));

                    vertices[i] += position;
                }
            }

            bounds = BoundingBox.CreateFromPoints(vertices);
        }
    }
}
